﻿/*************************************************************************************
 * 文 件 名:   HK_HierarchySearch
 * 
 * 描    述:   根据条件（名字、组件），对Hierarchy中选择的物体进行查询并重新选定
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭
 * 创建时间：  2023/8/26
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace HKTools
{
    public class HK_HierarchySearch : EditorWindow
    {
        const int HierarchySearchPriority = 25;

        //选择了弹出window复选框下的哪个
        int popupIndex = 0;
        string[] popupNameArray = { "NameSearch", "ComponentSearch" };
        int[] popupIndexArray = { 0, 1, };

        //方便以后维护,保证上面两个数组顺序和枚举里的顺序对应即可
        enum PopupIndex { NameSearch, ComponentSearch };

        bool ignoreCase = false;

        string searchName;
        string componentName;

        List<GameObject> go_list = new List<GameObject>();

        [MenuItem("HKTools/Hierarchy Search(查找物体工具)", priority = HierarchySearchPriority)]
        static void Init()
        {
            HK_HierarchySearch findGosWindow = GetWindow<HK_HierarchySearch>("Hierarchy Search");
            findGosWindow.Show();
        }

        void OnGUI()
        {
            EditorGUILayout.Space(10);

            EditorGUILayout.LabelField("Tips:");
            EditorGUILayout.LabelField("1.该工具用来查找Hierarchy中选中物体中，符合条件的物体");
            EditorGUILayout.LabelField("2.请先在Hierarchy中选择物体，再指定条件进行选择");
            EditorGUILayout.LabelField("3.根据组件名称进行查找时，比如输入正确的大小写");

            EditorGUILayout.Space(2);

            EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);

            EditorGUILayout.Space(3);

            popupIndex = EditorGUILayout.IntPopup("请选择功能:", popupIndex, popupNameArray, popupIndexArray);

            EditorGUILayout.Space(3);

            if (popupIndex == (int)PopupIndex.NameSearch)
            {
                ignoreCase = EditorGUILayout.Toggle("是否忽略大小写：", ignoreCase);

                EditorGUILayout.Space(5);

                searchName = EditorGUILayout.TextField("物体名称：", searchName);

                EditorGUILayout.Space(10);

                if (GUILayout.Button("查找并选择"))
                {
                    if (Selection.gameObjects.Length == 0)
                    {
                        Debug.LogWarning("Hierarchy中，没有选中的物体！");
                        return;
                    }

                    go_list.Clear();

                    for (int i = 0; i < Selection.gameObjects.Length; i++)
                    {
                        if (ignoreCase)
                        {
                            if (Selection.gameObjects[i].name.ToLower() == searchName.ToLower())
                            {
                                go_list.Add(Selection.gameObjects[i]);
                            }
                        }
                        else
                        {
                            if (Selection.gameObjects[i].name == searchName)
                            {
                                go_list.Add(Selection.gameObjects[i]);
                            }
                        }
                    }

                    if (go_list.Count > 0)
                    {
                        Selection.objects = go_list.ToArray();

                        go_list.Clear();
                    }
                    else
                    {
                        Selection.activeGameObject = null;

                        go_list.Clear();
                    }
                }
            }
            else if (popupIndex == (int)PopupIndex.ComponentSearch)
            {
                componentName = EditorGUILayout.TextField("组件名称：", componentName);

                EditorGUILayout.Space(10);

                if (GUILayout.Button("查找并选择"))
                {
                    if (Selection.gameObjects.Length == 0)
                    {
                        Debug.LogWarning("Hierarchy中，没有选中的物体！");
                        return;
                    }

                    go_list.Clear();

                    for (int i = 0; i < Selection.gameObjects.Length; i++)
                    {
                        if (Selection.gameObjects[i].GetComponent(componentName) != null)
                        {
                            go_list.Add(Selection.gameObjects[i]);
                        }
                    }

                    if (go_list.Count > 0)
                    {
                        Selection.objects = go_list.ToArray();

                        go_list.Clear();
                    }
                    else
                    {
                        Selection.activeGameObject = null;

                        go_list.Clear();
                    }
                }
            }
        }
    }
}